<template>
  <div class="myDiv">
    <Header title="自定义系统图标"></Header>
    <div class="uploader">
      <van-field name="uploader" label="公司logo">
        <template #input>
          <van-uploader
            v-model="form.fileList"
            :before-read="beforeRead"
            :after-read="afterRead"
            :max-count="1"
          />
        </template>
      </van-field>
    </div>
    <van-divider
      :style="{ color: '#f44336', borderColor: '#1989fa', padding: '0 16px' }"
    >提示:仅支持png/jpg格式的图片</van-divider>
    <van-field required v-model="form.systemName" label="系统名称" placeholder="请输入系统名称" />
    <van-field required v-model="form.companyName" label="公司名称" placeholder="请输入公司名称" />
    <van-field required v-model="form.companyAddress" label="公司地址" placeholder="请输入公司地址" />
    <van-field required v-model="form.companyPhone" label="公司电话" placeholder="请输入公司电话" />
    <van-field required v-model="form.companyUrl" label="公司网站" placeholder="请输入公司网站" />
    <div style="margin: 16px;">
      <van-button round block type="info" @click="onSubmit" native-type="submit">提交</van-button>
    </div>
  </div>
</template>

<script>
import Header from "@/components/header";
import Axios from "axios";

export default {
  components: { Header },
  props: {},
  data() {
    return {
      file: "",
      form: {
        fileList: [],
        companyName: "", //公司名称:"",
        systemName: "", //系统名称
        companyPhone: "", //公司电话
        companyUrl: "", //公司网址
        companyAddress: "" //公司地址
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    // 上传前校验图片格式
    beforeRead(file) {
      console.log(file.type);
      if (file.type !== "image/png" && file.type !== "image/jpeg") {
        this.Tips("请上传 png/jpg 格式图片");
        return false;
      }
      return true;
    },
    // 读取文件流
    afterRead(file) {
      this.file = file.file;
    },
    // 执行上传
    uploadImg() {
      //创建form对象
      let formdata = new FormData();
      // 通过append向form对象添加数据，可以通过append继续添加数据
      formdata.append("img", this.file);
      this.$http(this.api.customIconUpload, formdata).then(res => {
        if (res.data.result == "true") {
          this.Tips("上传成功！", "success");
        }
      });
    },
    onSubmit() {
      this.uploadImg();
      this.$http(this.api.customCompanyName, this.form).then(res => {
        if (res.data.result == "true") this.Tips("保存成功", "success");
      });
    }
  },
  created() {
    if (localStorage.getItem("company")) {
      var company = JSON.parse(localStorage.getItem("company"));
      this.form.companyPhone = company.companyPhone;
      this.form.companyAddress = company.companyAddress;
      this.form.companyName = company.companyName;
      this.form.systemName = company.systemName;
      this.form.companyUrl = company.companyUrl;
      this.form.fileList.push({
        url: `http://jk125.com/neoinfo/${company.iconUrl}`
      });
    }
  },
  mounted() {}
};
</script>
<style scoped>
.uploader {
  margin-top: 20px;
}
>>> .van-cell__value {
  overflow: visible;
}
</style>